package DynamicCrawler;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.ArrayList;

import static DynamicCrawler.TestUrl.testWsdlConnection;

public class SeleniumCrawler {
    static String chromeDriver = "chromedriver.exe";

    public static void scroll(ChromeDriver driver, int Speed) throws InterruptedException {
        int count = 0;
        //拉滚动条到最底部
        String height = String.valueOf(((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight"));
        for(int i = Speed; i<Integer.valueOf(height);i+=Speed){
            ((JavascriptExecutor) driver).executeScript("scrollTo(0," + String.valueOf(i) + ")");
            if(i >= Integer.valueOf(height) - Speed) {
                Thread.sleep(850);
                height = String.valueOf(((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight"));
                count++;
            }
/*            if(count > 5){
                break;
            }*/

        }
        ((JavascriptExecutor) driver).executeScript("scrollTo(0," + String.valueOf(0) + ")");
    }

    public static ArrayList<String> getElement(String url) throws Exception {
        ArrayList<String> list = new ArrayList<>();
        //配置chromedriver和chrome的路径
        System.setProperty("webdriver.chrome.driver",chromeDriver);//chromedriver服务地址

        //配置ChromeDriver启动参数，关闭Chrome自动化提示
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("useAutomationExtension", false);
        options.addArguments("disable-infobars");
        //options.setHeadless(true);
        //创建ChromeDriverEx对象，ChromeDriverEx对ChromeDriver进行扩展。
        ChromeDriver driver = new ChromeDriver(options);

        //打开网页
        driver.get(url);
        scroll(driver, 200);
        Document doc = Jsoup.parse(driver.getPageSource());
        Elements hrefs = doc.select("a[href]");
        for (Element elem : hrefs) {
           list.add(elem.attr("abs:href"));
        }
        driver.quit();
        return list;
    }

    public static void main(String[] args) throws Exception {
        String url = "http://db.auto.sina.com.cn/car_manual/242/";
        ArrayList<String> list = getElement(url);
        for(int i = 0; i < list.size(); i ++){
            if (list.get(i) == null){
                continue;
            }
            if (list.get(i).equals("")){
                continue;
            }
            if(!list.get(i).substring(0,1).equals("h")){
                continue;
            }
                System.out.println(testWsdlConnection(list.get(i)) + " " + list.get(i));
        }
    }
}
